package com.shxy

import com.shxy.dao.UserDao
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.test.web.servlet.MockMvc
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
import org.springframework.test.web.servlet.result.MockMvcResultHandlers
import org.springframework.test.web.servlet.result.MockMvcResultMatchers
import org.springframework.test.web.servlet.setup.MockMvcBuilders
import org.springframework.web.context.WebApplicationContext
import kotlin.test.assertEquals

@RunWith(SpringRunner::class)
@SpringBootTest
class HelloTest {

    @Autowired
    lateinit var userDao: UserDao
    @Autowired
    lateinit var wac:WebApplicationContext
    lateinit var mockMvc: MockMvc

    @Before
    fun before(){
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    @Test
    fun testUserDao(){
        val user = userDao.getUserById(1)
        println(user.username)
    }

    @Test
    fun testUserController(){
        val mvcResult = mockMvc.perform(
            MockMvcRequestBuilders
                .get("/user/tt")
                .contentType(MediaType.APPLICATION_FORM_URLENCODED))
            .andExpect(MockMvcResultMatchers.status().isOk)
            .andDo(MockMvcResultHandlers.print())
            .andReturn()
        println(mvcResult.response.contentAsString)
    }
}
